package com.itheima.tlias84.controller;


import com.itheima.tlias84.dto.SelectAllClazzDTO;
import com.itheima.tlias84.entity.Clazz;
import com.itheima.tlias84.po.InsertClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.service.impl.ClazzServiceImpl;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Resource
    ClazzServiceImpl clazzService;

    @GetMapping( "/list")
    public ResultUtil selectAll(){
        return ResultUtil.success(clazzService.selectAll());
    }
    @GetMapping
    public ResultUtil selectAllclazzs(SelectClazzPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())) {
            return ResultUtil.fail("分页参数不存在");
        }
        return clazzService.selectAllclazzs(param);
    }

    @DeleteMapping("{id}")
    public ResultUtil deleteById(@PathVariable("id") Integer id){
        log.info("id是{}",id);
        int i = clazzService.deleteById(id);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @PostMapping
    public ResultUtil insertClazz(@RequestBody InsertClazzParam param){
        int i = clazzService.insertClazz(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @GetMapping("/{id}")
    public ResultUtil selectClazzById(@PathVariable("id") Integer id){
        return ResultUtil.success(clazzService.selectClazzById(id));
    }

    @PutMapping
    public ResultUtil updateClazz(@RequestBody UpdateClazzParam param){
        int i = clazzService.updateClazz(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }


}
